home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / gc-nov90.lha / gc-26nov90 / expand.C < prev    next >
C/C++ Source or Header  |  1990-11-01  |  1KB  |  68 lines

  1. /* This program is used to test the ability of gcalloc to recover from heap
  2.    allocation failures.  It is run:
  3.  
  4.     expand [ <initial heap> [ <max heap> [ <increment> ] ] ]
  5.  
  6.    where all values are optional and expressed in megabytes.
  7. */
  8.  
  9. #include <string.h>
  10. #include <stream.h>
  11. #include <ctype.h>
  12.  
  13. #include  "gcalloc.C"
  14.  
  15. #define MB  1048576
  16.  
  17. /* The basic data structure is a list of blocks.  */
  18.  
  19. struct  block  {
  20.     block*  prev;
  21.     int  number[ 25000 ];
  22.     block( block* ptr, int x );
  23.     GCCLASS( block );
  24. };
  25.  
  26. void  block::GCPointers( )  {
  27.     gcpointer( prev );
  28. }
  29.  
  30. block::block( block* ptr, int x )
  31. {
  32.     GCALLOC( block );
  33.     prev = ptr;
  34.     for  (int i = 0 ; i < 25000 ; i++)  number[ i ] = x;
  35. }
  36.  
  37. void  makeheap( int initial, int final, int inc )
  38. {
  39.     gcheap  heap( initial*MB, final*MB, inc*MB, 0, 1, GCSTATS );
  40. }
  41.  
  42. main( int argc, char* argv[] )
  43. {
  44.     block  *lp = NULL;
  45.     int  i;
  46.  
  47.     makeheap( (argc == 1) ? 1 : atoi( argv[ 1 ] ),
  48.           (argc < 3) ? 1000 : atoi( argv[ 2 ] ),
  49.           (argc < 4) ? 2 : atoi( argv[ 3 ] ) );
  50.     for  (i = 0 ; expandfailed != 1 &&  i < 20; i++ )  {
  51.        lp = new block( lp, i );
  52.        gccollect();
  53.     }
  54.     lp = new block( lp, i );
  55.     gccollect();
  56.     lp = new block( lp, i+1 );
  57.     gccollect();
  58.     i = 0;
  59.     while  (lp != NULL)  {
  60.        cout << lp->number[ 0 ] << " " << lp->number[ 24999 ] << "  ";
  61.        lp = lp->prev;
  62.        if  (++i % 10 == 0)  cout << "\n";
  63.     }
  64.     cout << "\n";
  65.     exit( 0 );
  66. }
  67.  
  68.